//
//  HWAssociatedPhoneViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/29.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWAssociatedPhoneViewController.h"
#import "HWGuanlianPhoneTableViewCell.h"
#import "Countdown.h"

@interface HWAssociatedPhoneViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) UIView *lowerPartView;

@property(strong,nonatomic) UITextField *oneTextField;  // 第一个
@property(strong,nonatomic) UITextField *twoTextfeild;  //第二个
@property(strong,nonatomic) NSString *dataStr; // 验证码返回的参数

@end

@implementation HWAssociatedPhoneViewController

-(UITableView *)mainaTableVie {

    if  (_mainaTableVie == nil) {

        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, CF_NavHeight+88) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
       // _mainaTableVie.backgroundColor = RGB(241, 242, 244);
        
    }
    return   _mainaTableVie ;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
    
}


//设置UI
-(void)setupUI {
    self.navigationItem.title = @"关联手机";
    self.view.backgroundColor = [UIColor whiteColor];
   // self.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.mainaTableVie];
     self.dataStr = @"";
    
    [self.mainaTableVie mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(88);
        make.height.mas_equalTo(100);
    }];

    [self addLowerPartView];

}


// 发送邮箱验证吗
- (void)guanlianyouxiang {
    

    
     NSString *userID = [HWLocallyStoredData getLoginUserID];
     NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
     NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
     NSString *signMD5 = [WMUtil md5HexDigest:sign];
     NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"bind_code",
                                @"ios" : @"1",
                                @"uid" :userID,
                                @"t" :tStr,
                                @"sign": signMD5,
                                @"phone": self.oneTextField.text
                                };
    [MBProgressHUD show:@"正在发送验证码"];
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
     //   [MBProgressHUD hideHUD];
        
        
        if ([codeStr intValue] == 1) {
          
            //  response[@"data"];
            
          //  [MBProgressHUD showSuccess:@"验证码发送成功"];
            
            [MBProgressHUD  showSuccess:response[@"message"]];
            self.dataStr =  [NSString stringWithFormat:@"%@",response[@"data"]];
            
    
            
            
        } else {
            //  self.dataStr= @"";
             [MBProgressHUD  showError:response[@"message"]];
            
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
      
        [MBProgressHUD  showError:@"验证码发送失败"];
    
        
    }];
    
    
    
}








// 添加低部UIview
- (void)addLowerPartView {
    
    self.lowerPartView = [[UIView alloc]initWithFrame:CGRectMake(0, CF_NavHeight+88+30, kScreenW, 44)];
    // self.lowerPartView.backgroundColor = [UIColor yellowColor];
    self.lowerPartView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.lowerPartView];
    
    UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(15, 0, kScreenW-15*2, 36) Title:@"绑定" Image:nil Font:13 Color:RGB(255, 255, 155) Target:self Selector:@selector(buttonTap)];
    butt.backgroundColor = blueBackgroundBT;
    butt.layer.cornerRadius = 18;
    butt.layer.masksToBounds = YES;
    butt.backgroundColor  = [UIColor bm_colorGradientChangeWithSizeView:butt];
    
    [self.lowerPartView addSubview:butt];
    
    [self.lowerPartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
        make.top.mas_equalTo(self.mainaTableVie.mas_bottom).offset(30);
    }];

    
}

- (void)buttonTap {
    
    NSLog(@"保存");
//    HWbangdingQQViewController *bangding = [[HWbangdingQQViewController alloc]init];
//    [self.navigationController pushViewController:bangding animated:YES];
    
    
    if (!(self.oneTextField.text.length > 0)) {
              [MBProgressHUD showError:@"手机号不能为空"];
              return;
       } else if (!(self.twoTextfeild.text.length > 0)) {
           
           
            [MBProgressHUD showError:@"验证码不能为空"];
             return;
           
           
       }
       
       
         NSString *userID = [HWLocallyStoredData getLoginUserID];
         NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
         NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
         NSString *signMD5 = [WMUtil md5HexDigest:sign];
         NSDictionary *parametes = @{
                                    @"ct" :  @"app",
                                    @"ac" : @"bind_phone",
                                    @"ios" : @"1",
                                    @"code" : self.twoTextfeild.text,
                                    @"sessionid" : self.dataStr,
                                    @"uid" :userID,
                                    @"t" :tStr,
                                    @"sign": signMD5,
                                    @"phone": self.oneTextField.text
                                    };
        
        [MBProgressHUD show:@"正在绑定手机号"];
        
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
            
          
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
         //   [MBProgressHUD hideHUD];
            
            
            if ([codeStr intValue] == 1) {
              
          
               [MBProgressHUD  showSuccess:response[@"message"]];
                [self.navigationController popViewControllerAnimated:YES];
                
                
                

            } else {
             
                 [MBProgressHUD  showSuccess:response[@"message"]];
                
            }
            NSLog(@"%@",response);
            
            
        } failure:^(NSString *error) {

            [MBProgressHUD  showError:@"绑定邮箱成功"];
           
        }];
        
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    static NSString *cellID = @"cellID";
    
    HWGuanlianPhoneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[HWGuanlianPhoneTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
    }
    
    if (indexPath.row == 0) {
        
        cell.namelabel.text = @"手机号码";
        cell.codeBut.hidden = YES;
        
       NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"请输入手机号码" attributes:
                   @{NSForegroundColorAttributeName:RGB(153, 153, 153),
                     NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
                   ];
      cell.titleTextField.attributedPlaceholder  = attrString;
     self.oneTextField = cell.titleTextField;
        
    } else if (indexPath.row == 1) {
        
         cell.namelabel.text = @"验证码";
         cell.codeBut.hidden = NO;
         NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"请输入验证码" attributes:
                         @{NSForegroundColorAttributeName:RGB(153, 153, 153),
                           NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
                         ];
         cell.titleTextField.attributedPlaceholder  = attrString;
         self.twoTextfeild = cell.titleTextField;
        
    }
    
    [cell.codeBut addTarget:self action:@selector(xianshyanjTap:) forControlEvents:UIControlEventTouchUpInside];

    return  cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return  44;
    
}



// 获取验证码
- (void)xianshyanjTap:(UIButton *)sende {
    
//    if (![WMUtil validateMobile:self.titleTextField.text]) {
//
//        [MBProgressHUD showError:@"请输入正确的手机号"];
//        return ;
//
//
//    }
//

    
    [Countdown  StartTimeCodeBtn:sende];
    
    if (!(self.oneTextField.text.length > 0)) {
         [MBProgressHUD showError:@"手机号不能为空"];
         return;
     }
    
     
     [self guanlianyouxiang];
    
    
    
   
    
    
    
    DLog(@"获取验证码");
    

    
}



 
@end
